//
//  CollectionViewController.m
//  CollectionViewDemo
//
//  Created by liuchengming on 16/12/7.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "ReadingView.h"
#import "Parsing.h"
#import "TextViewController.h"


@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITextViewDelegate>
{
    NSArray<NSURL *> * texts ;
    NSMutableArray * bookName;
}
@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getBookName];
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    UIImage * backgroundimage = [UIImage imageNamed:@"1"];
    UIColor * backgroundColor = [UIColor colorWithPatternImage:backgroundimage];
    self.collectionView.backgroundColor = backgroundColor;
    self.navigationItem.title = @"1111";
    self.navigationController.toolbarHidden = YES;
    
    
    // [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tangyi" ofType:@".txt"] encoding:NSUTF8StringEncoding error:nil];
    //Parsing * par = [Parsing new];
    //NSString * str = [par ParsingText:nil];
    //NSLog(@"%@",str);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return bookName.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"style" forIndexPath:indexPath];
    
    cell.label.text = bookName[indexPath.row];
    //NSLog(@"=%@",bookName[indexPath.row]);
    NSString * imgname = [NSString stringWithFormat:@"%@.jpeg",bookName[indexPath.row]];
    cell.img.image = [UIImage imageNamed:imgname];
    
    
    return cell;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"现在是点击%@",indexPath);
    ReadingView * view = [ReadingView new];
    //view.view.backgroundColor = [UIColor blueColor];
    [view Textdata:indexPath.row];
    
    TextViewController * con = [TextViewController new];
    [self.navigationController pushViewController:con animated:YES];
    //[self presentViewController:view animated:YES completion:^{ }];
    
}

#pragma mark <Layout>

//cell大小
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(75, 100);
}

//整体边距
 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(30, 16, 0, 16);
}

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 30;
}
/*
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
 */

//获取书的ID
- (void)getBookName
{
    NSArray <NSURL *> * jpg = [[NSBundle mainBundle] URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    NSString * str = [NSString stringWithFormat:@"%@",jpg];
    NSArray * arrs = [str componentsSeparatedByString:@","];
    //NSLog(@"%@",stri);
    bookName = [NSMutableArray new];
    for (NSString * line in arrs)
    {
        NSArray * arr = [line componentsSeparatedByString:@".jpeg"];
        NSString * str = [NSString stringWithFormat:@"%@",arr.firstObject];
        arr = [str componentsSeparatedByString:@" "];
        str = [NSString stringWithFormat:@"%@",arr];
        arr = [str componentsSeparatedByString:@"\""];
        str = [NSString stringWithFormat:@"%@",arr[10]];
        [bookName addObject:str];
        //NSLog(@"%lu",(unsigned long)bookName.count);
        NSLog(@"%@",str);
    }
}

@end
